using System;
using System.Collections.Generic;
using UnityEngine;

namespace NodeCanvas.DialogueTrees
{
	[AddComponentMenu("")]
	public class DialogueTree : Graph
	{
		public enum EndState
		{
			Failure = 0,
			Success = 1,
			None = 3
		}

		public EndState endState = EndState.None;

		[SerializeField]
		private List<string> _dialogueActorNames = new List<string>();

		public Dictionary<string, DialogueActor> actorReferences = new Dictionary<string, DialogueActor>();

		private DLGNodeBase _currentNode;

		public DLGNodeBase currentNode
		{
			get
			{
				return _currentNode;
			}
			set
			{
				_currentNode = value;
			}
		}

		public List<string> dialogueActorNames
		{
			get
			{
				return _dialogueActorNames;
			}
			set
			{
				dialogueActorNames = value;
			}
		}

		public override Type baseNodeType
		{
			get
			{
				return typeof(DLGNodeBase);
			}
		}

		protected override bool allowNullAgent
		{
			get
			{
				return true;
			}
		}

		protected override void OnGraphStarted()
		{
			if (base.agent != null)
			{
				DialogueActor component = base.agent.GetComponent<DialogueActor>();
				if (component == null)
				{
					Debug.Log("Dialogue Agent has no Dialogue Actor. Adding one now...", base.agent.gameObject);
					component = base.agent.gameObject.AddComponent<DialogueActor>();
					component.blackboard = component.GetComponent<Blackboard>();
					if (component.blackboard == null)
					{
						Debug.Log("Dialogue Agent game object has now Blackboard. Adding one now and assigning it to the Dialogue Actor...", base.agent.gameObject);
						component.blackboard = component.gameObject.AddComponent<Blackboard>();
					}
					component.actorName = component.gameObject.name;
					base.agent = component;
				}
			}
			else
			{
				Debug.Log("Dialogue Started with no Agent to be used as 'Owner'. A default one will be created now...", base.gameObject);
				DialogueActor dialogueActor = base.gameObject.AddComponent<DialogueActor>();
				dialogueActor.blackboard = base.gameObject.AddComponent<Blackboard>();
				dialogueActor.actorName = "Default";
				base.agent = dialogueActor;
			}
			if (base.blackboard == null)
			{
				base.blackboard = (base.agent as DialogueActor).blackboard;
			}
			actorReferences.Clear();
			foreach (string dialogueActorName in dialogueActorNames)
			{
				actorReferences[dialogueActorName] = DialogueActor.FindActorWithName(dialogueActorName);
			}
			foreach (KeyValuePair<string, DialogueActor> actorReference in actorReferences)
			{
				if (actorReference.Value == null)
				{
					Debug.LogWarning(actorReference.Key + " DialogueActor not found when starting Dialogue Tree " + base.graphName + ". Dialogue will play but if a node with that actor is encountered an error will show and the Dialogue will Stop", base.gameObject);
				}
			}
			foreach (DLGNodeBase allNode in base.allNodes)
			{
				allNode.SendTaskOwnerDefaults();
			}
			EventHandler.Dispatch(DLGEvents.OnDialogueStarted, this);
			currentNode = ((!(currentNode != null)) ? ((DLGNodeBase)base.primeNode) : currentNode);
			currentNode.Execute();
		}

		protected override void OnGraphStoped()
		{
			endState = (EndState)((!currentNode) ? Status.Success : currentNode.status);
			EventHandler.Dispatch(DLGEvents.OnDialogueFinished, this);
			actorReferences.Clear();
			currentNode = null;
		}

		protected override void OnGraphPaused()
		{
			EventHandler.Dispatch(DLGEvents.OnDialoguePaused, this);
		}
	}
}
